.PHONY: all check clean dist distclean install install-headers regressions uninstall

BUILD_DIR=@BUILD_DIR@
SRC_DIR=@SRC_DIR@
CFLAGS=@CFLAGS@
VERSION=@VERSION@
VERSION_MAJOR=@VERSION_MAJOR@
PREFIX=@PREFIX@
LIBRARY=@LIBRARY@
HEADERS=@HEADERS@
PKGCONFIG_DATA=@LIBRARY@/pkgconfig
LDNAME=@LDNAME@
LDNAME_VERSION=@LDNAME_VERSION@
LDNAME_MAJOR=@LDNAME_MAJOR@

all:
	$(MAKE) -C src all || exit
	@echo
	@echo
	@echo ---[ libelfmaster has built successfully. You may now \"make install\".

regressions:
	$(MAKE) -C regressions all || exit
	@echo ---[ Regressions have built successfully.

check: regressions
	@echo ---[ Executing unit tests...
	$(MAKE) -C regressions check
	@echo ---[ Unit tests have completed successfully.

install-headers:
	mkdir -p $(DESTDIR)/$(HEADERS) || exit
	cp $(SRC_DIR)/include/*.h $(DESTDIR)/$(HEADERS) || exit
	chmod 755 $(DESTDIR)/$(HEADERS) || exit
	chmod 644 $(DESTDIR)/$(HEADERS)/*.h || exit

install: all install-headers
	mkdir -p $(DESTDIR)/$(LIBRARY) || exit
	mkdir -p $(DESTDIR)/$(PREFIX)/bin || exit
	mkdir -p $(DESTDIR)/$(PKGCONFIG_DATA) || exit
	chmod 755 $(DESTDIR)/$(PKGCONFIG_DATA)
	cp build/libelfmaster.pc $(DESTDIR)/$(PKGCONFIG_DATA)/libelfmaster.pc || exit
	cp src/libelfmaster.a $(DESTDIR)/$(LIBRARY)/libelfmaster.a || exit
	chmod 644 $(DESTDIR)/$(LIBRARY)/libelfmaster.a || exit
	@echo
	@echo
	@echo ---[ libelfmasterhas installed successfully.

uninstall:
	rm -f $(DESTDIR)/$(LIBRARY)/$(LDNAME_VERSION)	\
	      $(DESTDIR)/$(LIBRARY)/$(LDNAME)		\
	      $(DESTDIR)/$(LIBRARY)/$(LDNAME_MAJOR)
	rm -f $(DESTDIR)/$(LIBRARY)/libelfmaster.so*
	rm -f $(DESTDIR)/$(LIBRARY)/libelfmaster.a
	rm -f $(DESTDIR)/$(HEADERS)/libelfmaster.h
	rm -f $(DESTDIR)/$(HEADERS)/elfmaster.h
	rm -f $(DESTDIR)/$(PKGCONFIG_DATA)/libelfmaster.pc

clean:
	$(MAKE) -C src clean
	rm -f $(BUILD_DIR)/*~ $(BUILD_DIR)/*.o $(BUILD_DIR)/*.tar.gz

dist:
	git archive --remote=$(SRC_DIR) --format=tar --prefix=libelfmaster-$(VERSION)/ HEAD \
		| gzip > $(BUILD_DIR)/libelfmaster-$(VERSION).tar.gz

distclean: clean
	rm -f $(BUILD_DIR)/build/regressions.build
	rm -f $(BUILD_DIR)/build/libelfmaster.build
	rm -f $(BUILD_DIR)/build/libelfmaster.pc
	rm -f $(BUILD_DIR)/Makefile

